<?php

namespace app\admin\controller\product;

use app\common\controller\Backend;

/**
 * 订单回收站管理 
 */
class Ordrecycle extends Backend
{
    /**
     * 是否是关联查询
     */
    protected $orderPromodel = null;
    protected $relationSearch = true;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Product.Order.Order');
        $this->orderPromodel = model('Product.Order.Product');
        $status = [
            '0' => '未支付',
            '1' => '已支付',
            '2' => '已发货',
            '3' => '已收货',
            '4' => '已完成',
            '-1' => '仅退款',
            '-2' => '退款退货',
            '-3' => '售后中',
            '-4' => '审核成功',
            '-5' => '审核失败'
        ];
        $this->view->assign('status', $status);
    }
    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->onlyTrashed()
                ->with(['expressquery', 'business'])
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->onlyTrashed()
                ->with(['expressquery', 'business'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }
    /**
     * 删除
     */
    public function del($ids = null)
    {
        $row = $this->model->onlyTrashed()->select($ids);
        if (empty($row)) {
            $this->error('当前订单不存在');
        }
        $orderpro = $this->orderPromodel->where('orderid', 'IN', $ids)->select();
        $result = $this->model->destroy($ids, true);
        if (!$result) {
            $this->error($this->model->getError());
        }
        if ($orderpro) {
            foreach ($orderpro as $item) {
                $imgs = $item['proimg'];
                if ($imgs) {
                    $imgs = explode(',', $imgs);
                    foreach ($imgs as $img) {
                        delImg($img);
                    }
                }
            }
        }
        $this->success();
    }
    /*
    还原
    */
    public function restore($ids = null)
    {
        $ids = explode(',', $ids);
        $dataList = [];
        foreach ($ids as $id) {
            $data = [
                'id' => $id,
                'deletetime' => null
            ];
            $dataList[] = $data;
        }
        $res = $this->model->isUpdate()->saveAll($dataList);
        if ($res == 0) {
            $this->error($this->model->getError());
        }
        $this->success();
    }
}
